﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Xml;
using SMSSnew.smss_web_reference;
using System.Collections;
using SMSSnew.BIZ;

namespace SMSSnew.DATA.CLASSES
{

    public class subjectSubTypesCLZ : subjectCLZ
    {

        static smss_web_service_app srv = new smss_web_service_app();
        static readXML XMLreader = new readXML();

        DataTable dt;


        string subject_sub_type_name;
        public string subject_sub_type_id;

        public subjectSubTypesCLZ()
        {
        }

        public subjectSubTypesCLZ(String subject_code)
        {
        }

        public subjectSubTypesCLZ(string subject_sub_type_id)
        {
            this.subject_sub_type_id = subject_sub_type_id;
        }

        public void setSubjectName(string subjectName)
        {

            this.subject_name = subjectName;
        }

        public void setSubjectTypeID(string subject_sub_type_id)
        {
            this.subject_sub_type_id = subject_sub_type_id;
        }


        subjectSubTypesCLZ(String subject_name, String subject_code, int subject_weight, int total_slots,
        string subject_sub_type_name, string subject_sub_type_id, string grade_id)
        {
            this.subject_name = subject_name;
            this.subject_code = subject_code;
            this.subject_weight = subject_weight;
            this.total_slots = total_slots;
            this.grade_id = grade_id;

            this.subject_sub_type_name = subject_sub_type_name;
            this.subject_sub_type_id = subject_sub_type_id;
        }

        //returns subject_sub_type_id
        public string getSubTypeID(string subjectName, string subjectSubTypeName)
        {
            string ID;
            DataTable dt;
            string query1, query2, query3;


            try
            {
                if (noOfSubTypes(subjectName) > 1)
                {

                    query1 = XMLreader.readXMLtxt("getSubjectSubTypeID_1", "~/DATA/XML/subjectXML.xml");
                    query1 = query1.Replace("@subjectName", subjectName);
                    srv.Modify(query1);

                    query2 = XMLreader.readXMLtxt("getSubjectSubTypeID_2", "~/DATA/XML/subjectXML.xml");
                    query2 = query2.Replace("@subjectSubTypeName", subjectSubTypeName);
                    dt = srv.Search(query2);

                    query3 = XMLreader.readXMLtxt("getSubjectSubTypeID_3", "~/DATA/XML/subjectXML.xml");
                    srv.Modify(query3);
                    ID = dt.Rows[0][0].ToString();
                }
                else
                {
                    query1 = XMLreader.readXMLtxt("getSubjectSubTypeID_4", "~/DATA/XML/subjectXML.xml");
                    query1 = query1.Replace("@subjectName", subjectName);
                    dt = srv.Search(query1);
                    ID = dt.Rows[0][0].ToString();
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ID;

        }

        //method for adding subject details 

        public string createSubjectDetails(string subject_name, string subject_code, string subject_sub_type_name, string subject_sub_type_id, int subject_weight,
            bool grade_6, bool grade_7, bool grade_8, bool grade_9, bool grade_10, bool grade_11, int total_slots_6, int total_slots_7,
            int total_slots_8, int total_slots_9, int total_slots_10, int total_slots_11)
        {
            string query1 = "", query2 = "", query3 = "", query4 = "";
            string message = "";



            if (grade_6 == true || grade_7 == true || grade_8 == true || grade_9 == true || grade_10 == true || grade_11 == true)
            {

                if (checkForDuplicateSubjectTypeEntry(this.subject_sub_type_id) == true)
                {
                    message = "alert('The teacher is already assigned to a class as  a class teacher');";
                }

                else if (checkForDuplicateSubjectSubTypeEntry(this.subject_sub_type_id, this.grade_id, this.subject_code) == true)
                {
                    message = "alert('The teacher is already assigned to a class as  a class teacher');";
                }

                else if (checkForDuplicateSubjectTotalslotsWeightEntry(this.grade_id, this.subject_code) == true)
                {


                    message = "alert('The teacher is already assigned to a class as  a class teacher');";

                }

                else
                {
                    query2 = XMLreader.readXMLtxt("IinsertSubjectType", "~/DATA/XML/classXML.xml");
                    query2 = query2.Replace("@subject_sub_type_id", this.subject_sub_type_id);
                    query2 = query2.Replace("@subject_sub_type_name", this.subject_sub_type_name);
                    srv.Modify(query2);


                    if (grade_6 == true)
                    {
                        grade_id = "6";
                        total_slots = total_slots_6;

                        query3 = XMLreader.readXMLtxt("IinsertSubjectSubType", "~/DATA/XML/classXML.xml");
                        query3 = query3.Replace("@subject_type_id", this.subject_sub_type_id);
                        query3 = query3.Replace("@subject_code", this.subject_code);
                        query3 = query3.Replace("@grade_id", this.grade_id);
                        srv.Modify(query3);

                        query4 = XMLreader.readXMLtxt("IinsertSubjectTotalslotsWeight", "~/DATA/XML/classXML.xml");
                        query4 = query4.Replace("@grade_id", this.grade_id);
                        query4 = query4.Replace("@subject_code", this.subject_code);
                        query4 = query4.Replace("@subject_weight", Convert.ToString(this.subject_weight));
                        query4 = query4.Replace("@total_slots", Convert.ToString(this.total_slots));
                        srv.Modify(query4);
                    }

                    if (grade_7 == true)
                    {
                        grade_id = "7";
                        total_slots = total_slots_7;

                        query3 = XMLreader.readXMLtxt("IinsertSubjectSubType", "~/DATA/XML/classXML.xml");
                        query3 = query3.Replace("@subject_type_id", this.subject_sub_type_id);
                        query3 = query3.Replace("@subject_code", this.subject_code);
                        query3 = query3.Replace("@grade_id", this.grade_id);
                        srv.Modify(query3);

                        query4 = XMLreader.readXMLtxt("IinsertSubjectTotalslotsWeight", "~/DATA/XML/classXML.xml");
                        query4 = query4.Replace("@grade_id", this.grade_id);
                        query4 = query4.Replace("@subject_code", this.subject_code);
                        query4 = query4.Replace("@subject_weight", Convert.ToString(this.subject_weight));
                        query4 = query4.Replace("@total_slots", Convert.ToString(this.total_slots));
                        srv.Modify(query4);
                    }

                    if (grade_8 == true)
                    {
                        grade_id = "8";
                        total_slots = total_slots_8;

                        query3 = XMLreader.readXMLtxt("IinsertSubjectSubType", "~/DATA/XML/classXML.xml");
                        query3 = query3.Replace("@subject_type_id", this.subject_sub_type_id);
                        query3 = query3.Replace("@subject_code", this.subject_code);
                        query3 = query3.Replace("@grade_id", this.grade_id);
                        srv.Modify(query3);

                        query4 = XMLreader.readXMLtxt("IinsertSubjectTotalslotsWeight", "~/DATA/XML/classXML.xml");
                        query4 = query4.Replace("@grade_id", this.grade_id);
                        query4 = query4.Replace("@subject_code", this.subject_code);
                        query4 = query4.Replace("@subject_weight", Convert.ToString(this.subject_weight));
                        query4 = query4.Replace("@total_slots", Convert.ToString(this.total_slots));
                        srv.Modify(query4);
                    }

                    if (grade_9 == true)
                    {
                        grade_id = "9";
                        total_slots = total_slots_9;

                        query3 = XMLreader.readXMLtxt("IinsertSubjectSubType", "~/DATA/XML/classXML.xml");
                        query3 = query3.Replace("@subject_type_id", this.subject_sub_type_id);
                        query3 = query3.Replace("@subject_code", this.subject_code);
                        query3 = query3.Replace("@grade_id", this.grade_id);
                        srv.Modify(query3);

                        query4 = XMLreader.readXMLtxt("IinsertSubjectTotalslotsWeight", "~/DATA/XML/classXML.xml");
                        query4 = query4.Replace("@grade_id", this.grade_id);
                        query4 = query4.Replace("@subject_code", this.subject_code);
                        query4 = query4.Replace("@subject_weight", Convert.ToString(this.subject_weight));
                        query4 = query4.Replace("@total_slots", Convert.ToString(this.total_slots));
                        srv.Modify(query4);
                    }

                    if (grade_10 == true)
                    {
                        grade_id = "10";
                        total_slots = total_slots_10;

                        query3 = XMLreader.readXMLtxt("IinsertSubjectSubType", "~/DATA/XML/classXML.xml");
                        query3 = query3.Replace("@subject_type_id", this.subject_sub_type_id);
                        query3 = query3.Replace("@subject_code", this.subject_code);
                        query3 = query3.Replace("@grade_id", this.grade_id);
                        srv.Modify(query3);

                        query4 = XMLreader.readXMLtxt("IinsertSubjectTotalslotsWeight", "~/DATA/XML/classXML.xml");
                        query4 = query4.Replace("@grade_id", this.grade_id);
                        query4 = query4.Replace("@subject_code", this.subject_code);
                        query4 = query4.Replace("@subject_weight", Convert.ToString(this.subject_weight));
                        query4 = query4.Replace("@total_slots", Convert.ToString(this.total_slots));
                        srv.Modify(query4);
                    }

                    if (grade_11 == true)
                    {
                        grade_id = "11";
                        total_slots = total_slots_11;

                        query3 = XMLreader.readXMLtxt("IinsertSubjectSubType", "~/DATA/XML/classXML.xml");
                        query3 = query3.Replace("@subject_type_id", this.subject_sub_type_id);
                        query3 = query3.Replace("@subject_code", this.subject_code);
                        query3 = query3.Replace("@grade_id", this.grade_id);
                        srv.Modify(query3);

                        query4 = XMLreader.readXMLtxt("IinsertSubjectTotalslotsWeight", "~/DATA/XML/classXML.xml");
                        query4 = query4.Replace("@grade_id", this.grade_id);
                        query4 = query4.Replace("@subject_code", this.subject_code);
                        query4 = query4.Replace("@subject_weight", Convert.ToString(this.subject_weight));
                        query4 = query4.Replace("@total_slots", Convert.ToString(this.total_slots));
                        srv.Modify(query4);
                    }




                }

                if (checkForDuplicateSubjectEntry(this.subject_code) == false)
                {
                    query1 = XMLreader.readXMLtxt("IinsertSubject", "~/DATA/XML/classXML.xml");
                    query1 = query1.Replace("@subject_code", this.subject_code);
                    query1 = query1.Replace("@subject_name", this.subject_name);
                    srv.Modify(query1);
                }
                else
                {
                }

            }


            else
            {
                message = "alert('Need total time slots!');";
            }


            try
            {
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return message;

        }


        //check for duplicate subject entry details

        public bool checkForDuplicateSubjectEntry(string subject_code)
        {
            string query3 = "";
            bool havSubject = false;
            //DataTable dt;
            try
            {

                query3 = XMLreader.readXMLtxt("checkForDuplicateSubjectEntry", "~/DATA/XML/TimetableDataEntry.xml");
                query3 = query3.Replace("@subject_code", subject_code);
                dt = srv.Search(query3);

                if (dt.Rows.Count != 0)
                    havSubject = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return havSubject;
        }


        //check for duplicate subject type entry details

        public bool checkForDuplicateSubjectTypeEntry(string subject_sub_type_id)
        {
            string query3 = "";
            bool havSubjectType = false;
            //DataTable dt;
            try
            {

                query3 = XMLreader.readXMLtxt("checkForDuplicateSubjectTypeEntry", "~/DATA/XML/TimetableDataEntry.xml");
                query3 = query3.Replace("@subject_sub_type_id", subject_sub_type_id);
                dt = srv.Search(query3);

                if (dt.Rows.Count != 0)
                    havSubjectType = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return havSubjectType;
        }


        //check for duplicate subject sub type entry details

        public bool checkForDuplicateSubjectSubTypeEntry(string subject_sub_type_id, string grade_id, string subject_code)
        {
            string query3 = "";
            bool havSubjectSubType = false;
            //DataTable dt;
            try
            {

                query3 = XMLreader.readXMLtxt("checkForDuplicateSubjectSubTypeEntry", "~/DATA/XML/TimetableDataEntry.xml");
                query3 = query3.Replace("@subject_sub_type_id", grade_id);
                query3 = query3.Replace("@grade_id", grade_id);
                query3 = query3.Replace("@subject_code", subject_code);
                dt = srv.Search(query3);

                if (dt.Rows.Count != 0)
                    havSubjectSubType = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return havSubjectSubType;
        }

        //check for duplicate Subject Total slots Weight Entry details

        public bool checkForDuplicateSubjectTotalslotsWeightEntry(string grade_id, string subject_code)
        {
            string query3 = "";
            bool havSubjectTotalslotsWeight = false;
            //DataTable dt;
            try
            {

                query3 = XMLreader.readXMLtxt("checkForDuplicateSubjectTotalslotsWeightEntry", "~/DATA/XML/TimetableDataEntry.xml");
                query3 = query3.Replace("@grade_id", grade_id);
                query3 = query3.Replace("@subject_code", subject_code);
                dt = srv.Search(query3);

                if (dt.Rows.Count != 0)
                    havSubjectTotalslotsWeight = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return havSubjectTotalslotsWeight;
        }

        //Load details in the database into interface(drop down list)

        //load grade id

        public DataTable LoadGradeID()
        {
            string query = "";
            try
            {

                query = XMLreader.readXMLtxt("LoadGradeID", "~/DATA/XML/TimetableXML.xml");
                dt = srv.Search(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        //load subject code

        public DataTable LoadSubjectCode(subjectSubTypesCLZ objClass)
        {
            string query = "";
            try
            {

                query = XMLreader.readXMLtxt("LoadSubjectCode", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@grade_id", objClass.grade_id);

                dt = srv.Search(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        //load subject type id

        public DataTable LoadSubjectTypeID(subjectSubTypesCLZ objClass)
        {
            string query = "";
            try
            {

                query = XMLreader.readXMLtxt("LoadSubjectTypeID", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@subject_code", objClass.subject_code);

                dt = srv.Search(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        //Method for deleting subject details

        public string deleteSubjectDetails(string grade_id, string subject_code, string subject_sub_type_id)
        {
            string message = "", query4 = "", query5 = "", query6 = "", query7 = "";



            if (checkForMultipleSubjectEntry(this.subject_code, this.grade_id) == true)
            {
                query4 = XMLreader.readXMLtxt("DeleteSubjectDetails", "~/DATA/XML/classXML.xml");
                query4 = query4.Replace("@subject_code", this.subject_code);
                srv.Modify(query4);

                query5 = XMLreader.readXMLtxt("DeleteSubjectTotalSlotsWeightDetails", "~/DATA/XML/classXML.xml");
                query5 = query5.Replace("@subject_code", this.subject_code);
                query5 = query5.Replace("@grade_id", this.grade_id);
                srv.Modify(query5);
            }

            if (checkForMultipleSubjectTypeEntry(this.subject_code, this.grade_id) == true)
            {
                query6 = XMLreader.readXMLtxt("DeleteSubjectTypeDetails", "~/DATA/XML/classXML.xml");
                query6 = query6.Replace("@subject_sub_type_id", this.subject_sub_type_id);
                srv.Modify(query6);
            }

            query7 = XMLreader.readXMLtxt("DeleteSubjectSubTypeDetails", "~/DATA/XML/classXML.xml");
            query7 = query7.Replace("@subject_sub_type_id", this.subject_sub_type_id);
            query7 = query7.Replace("@grade_id", this.grade_id);
            srv.Modify(query7);

            message = "alert('The subject details of the particular grade are deleted');";

            return message;

        }


        //check multiple subject entry

        public bool checkForMultipleSubjectEntry(string subject_code, string grade_id)
        {
            string query3 = "";
            bool havMultipleSubjectEntry = false;
            //DataTable dt;
            try
            {

                query3 = XMLreader.readXMLtxt("checkForMultipleSubjectEntry", "~/DATA/XML/TimetableDataEntry.xml");
                query3 = query3.Replace("@subject_code", subject_code);
                query3 = query3.Replace("@grade_id", grade_id);
                dt = srv.Search(query3);

                if (dt.Rows.Count == 1)
                    havMultipleSubjectEntry = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return havMultipleSubjectEntry;
        }


        //check multiple subject type entry

        public bool checkForMultipleSubjectTypeEntry(string subject_code, string grade_id)
        {
            string query3 = "";
            bool havMultipleSubjectTypeEntry = false;
            //DataTable dt;
            try
            {

                query3 = XMLreader.readXMLtxt("checkForMultipleSubjectTypeEntry", "~/DATA/XML/TimetableDataEntry.xml");
                query3 = query3.Replace("@subject_sub_type_id", subject_sub_type_id);
                dt = srv.Search(query3);

                if (dt.Rows.Count == 1)
                    havMultipleSubjectTypeEntry = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return havMultipleSubjectTypeEntry;
        }



        //load subject name for edit

        public DataTable LoadSubjectNameEdit()
        {
            string query = "";
            try
            {

                query = XMLreader.readXMLtxt("LoadSubjectNameEdit", "~/DATA/XML/TimetableXML.xml");


                dt = srv.Search(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        //load subject code for edit

        public DataTable LoadSubjectCodeForEdit(subjectSubTypesCLZ objClass)
        {
            string query = "";
            try
            {

                query = XMLreader.readXMLtxt("LoadSubjectCodeEdit", "~/DATA/XML/TimetableXML.xml");
                query = query.Replace("@subject_name", subject_name);


                dt = srv.Search(query);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

    }
}
